<?php
class User extends AppModel {
	var $name = 'User';
	var $displayField = 'name';
	var $validate = array(
		'username' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Must have username',
				'allowEmpty' => false,
			),
			'between' => array(
				'rule' => array('between', 5, 15),
				'message' => 'Username must be between 5 and 15 characters long',
			),
                        'unique' => array(
                                'rule'=>'isUnique',
                                'message'=>'Username has already been taken',
                        ),
		),
		'password' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Must have password',
				'allowEmpty' => false,
			),
			'between' => array(
				'rule' => array('minLength',6),
				'message' => 'Password must be a minimum of 6 characters long',
			),
                        'The passwords do not match' => array(
                                'rule'=>'matchPasswords',
                                'message'=>'The passwords do not match.',
                        ),
		),
                'password_confirmation' => array(
                        'password_confirmation' => array(
                                'rule' => 'notempty',
                                'message' => 'Must have a password',
                                'allowEmpty' => false,
                        ),
                ),
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Must have a name',
				'allowEmpty' => false,
			),
			'between' => array(
				'rule' => array('between', 3, 30),
				'message' => 'Name must be between 3 and 30 characters long',
			),
		),
		'address' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Must have an address',
				'allowEmpty' => false,
			),
		),
		'city' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Must have a city',
				'allowEmpty' => false,
			),
		),
		'province' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Must have a province',
				'allowEmpty' => false,
			),
		),
		'postal_code' => array(
 			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Must have a postal code',
				'allowEmpty' => false,
			),
		),
		'email_address' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Must have an e-mail address',
				'allowEmpty' => false,
			),
			'email' => array(
				'rule' => array('email'),
				'message' => 'E-mail address format incorrect',
			),
		),
//		'role' => array(
//			'notempty' => array(
//				'rule' => array('notempty'),
//				'message' => 'Must have a role',
//				'allowEmpty' => false,
//			),
//		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $hasAndBelongsToMany = array(
		'Order' => array(
			'className' => 'Order',
			'joinTable' => 'users_orders',
			'foreignKey' => 'user_id',
			'associationForeignKey' => 'order_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
        function matchPasswords($data) {
            if ($data['password'] == $this->data['User']['password_confirmation']) {
                return TRUE;
            }
            $this->invalidate('password_confirmation', 'The passwords do not match');
            return FALSE;
        }

        function hashPasswords($data) {
            if (isset($this->data['User']['password'])) {
                $this->data['User']['password'] = Security::hash($this->data['User']['password'], NULL, TRUE);
                return $data;
            }
            return $data;
        }

        function beforeSave() {
            $this->hashPasswords(NULL, TRUE);
            return TRUE;
        }
}
?>